import java.util.Scanner;

public class Main {


    //数组中两个字符串的最小距离
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n= in.nextInt();
        String s1 = in.next();
        String s2 = in.next();
        int pre1 = -1;
        int pre2 = -1;
        int ret = 0x3f3f3f3f;
        for (int i = 0; i < n; i++) {
            String s = in.next();
            if (s.equals(s1)){
                if (pre2 != -1){
                    ret = Math.min(ret,i-pre2);
                }
                pre1 = i;
            }else if(s.equals(s2)){
                if(pre1 != -1){
                    ret = Math.min(ret , i-pre1);
                }
                pre2 = i;
            }
        }
        System.out.println(ret == 0x3f3f3f3f ? -1 : ret);
    }


    //最小花费爬楼梯
    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] cost = new int[n];
        int[] dp = new int[n+1];
        for (int i = 0; i <= n; i++) {
            cost[i] = in.nextInt();
        }
        dp[0] = 0;
        dp[1] = 0;
        for (int i = 2; i < n; i++) {
            dp[i] = Math.min(dp[i-1] +cost[i-1],dp[i-2]+cost[i-2]);
        }
        System.out.println(dp[n]);
    }


    //牛牛的快递
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        double n = in.nextDouble();
        char ch = in.next().charAt(0);

        int ret = 0;

        if(n<1) ret = 20;
        else ret = 20 + (int)Math.ceil(n-1);

        if (ch == 'y') ret += 5;

        System.out.println(ret);
    }
}